﻿

TT.Tournament = {};

TT.Tournament.Init = function(){
     
    // Suffix each stage alphabetically, starging from the final outwards
    var letterCode = 65; // charcode for A
    var $finalRound = $("#knockout > .round:last-child");
    $finalRound.children(".stage").addClass("stage-" + String.fromCharCode(letterCode).toLowerCase() ); 
    letterCode++;
    $finalRound.prevAll(".round").each( function(){
        $(this).children(".stage").each(function(){
            $(this).addClass("stage-" + String.fromCharCode(letterCode).toLowerCase() + " position-" + $(this).index() ); 
        });
        letterCode++;
    });
    
    TT.Tournament.PositionBoard();  
    $(window).resize(function(){
        TT.Tournament.PositionBoard();
    })
    
}

TT.Tournament.PositionBoard = function(){

    var fontSize = parseInt($("#knockout").css("font-size"));

    var viewportWidth = $(window).width();
    var viewportheight = $(window).height();

    if($(window).width() < 780){
        var columnWidth = viewportWidth / 6;    
    }else{
        var columnWidth = viewportWidth / 7;    
    }

    var quarterHOffset = columnWidth * 3
    var quarterVOffset = viewportheight  / 3;


    var semiHOffset = columnWidth * 2 ;
    var semiVOffset = viewportheight / 4.5;

    var finalHOffset = columnWidth - 4;
    var finalVOffset = viewportheight / 9;


    $(".stage").width(columnWidth);
    $(".stage").height( viewportheight / 2 );

    $(".stage.stage-b").height( viewportheight / 2.35);
    $(".stage.stage-c").height( viewportheight / 4);
    
    $(".stage.stage-a").width(columnWidth*2).height( 4 );

    $(".stage.stage-c.position-0, .stage.stage-c.position-1").css( "left", -quarterHOffset );
    $(".stage.stage-c.position-2, .stage.stage-c.position-3").css( "right", -quarterHOffset );
    $(".stage.stage-c.position-0, .stage.stage-c.position-2").css( "top", -quarterVOffset );
    $(".stage.stage-c.position-1, .stage.stage-c.position-3").css( "bottom", -quarterVOffset );


    $(".stage.stage-b.position-0").css( { "left": -semiHOffset,  "top": -semiVOffset });
    $(".stage.stage-b.position-1").css( { "right": -semiHOffset,  "top": -semiVOffset });

    $(".stage.stage-a").css( { "left": -finalHOffset,  "top": -2 });

    // $(".player").each(function(){
    //     var height = $(this).height();
    //     if($(this).hasClass("a") || $(this).closest(".stage").hasClass("stage-a")){
    //     $(this).css("margin-top", -(height / 2));
    // }else{
    //     $(this).css("margin-bottom", -(height / 2));
    // }
    // });

};




$(function(){
    TT.Tournament.Init();
})